unit DataSetExport;

interface

uses
  SysUtils, Classes, DB, Math, Provider, DBClient;

type
  TDataSetExport = class(TComponent)
  private
    FTitle: string;
    FFileName: string;
    FDataSet: TDataSet;
    FBookmark: string;
    procedure Initialize;
    procedure Finalize;
  protected
    { Protected declarations }
  public
    procedure ExportToHTML;
    procedure ExportToTXT;
    procedure ExportToRTF;
    procedure ExportToXML;
  published
    property Title: string read FTitle write FTitle;
    property DataSet: TDataSet read FDataSet write FDataSet;
    property FileName: string read FFileName write FFileName;
  end;

implementation

{ TDataSetExport }

procedure TDataSetExport.ExportToHTML;
var
  Content: TStrings;
  I, J: Integer;
begin
  Initialize;
  Content := TStringList.Create;
  try
    Content.Add('<HTML><HEAD><TITLE>' + FTitle + '</TITLE></HEAD><BODY><B><font face="Tahoma" size="2">' + FTitle + '</B><BR>');
    Content.Add('<BR>');
    Content.Add('<TABLE BORDER=1><TR>');
    for i := 0 to FDataSet.FieldCount - 1 do
      Content.Add('<TD><B><font size="2">' + DataSet.Fields[i].DisplayLabel + '</B></TD>');
    Content.Add('</TR>');
    J := 0;
    while not DataSet.Eof do
    begin
      Content.Add('<TR>');
      for i := 0 to FDataSet.FieldCount - 1 do
        Content.Add('<TD><font size="2">' + FDataSet.Fields[i].DisplayText + '</TD>');
      Content.Add('</TR>');
      FDataSet.Next;
      Inc(J);
    end;
    Content.Add('</TABLE><BR><BR><B>Total de Registros: ' + IntToStr(J) + '</B></BODY></HTML>');
    Content.SaveToFile(FileName);
  finally
    Finalize;
    Content.Free;
  end;
end;

procedure TDataSetExport.ExportToRTF;
var
  Content: TStrings;
  y, x, i: Integer;
  s, Cabec: string;
begin
  Cabec := '{\rtf1\ansi\ansicpg1252\deff0\deflang1046{\fonttbl{\f0\fmodern\fprq1\fcharset0 Courier New;}{\f1\fswiss\fprq2\fcharset0 Arial;}}'#13#10 +
    '\viewkind4\uc1\pard\b\f0\fs20 %s\par'#13#10'\par';
  Initialize;
  Content := TStringList.Create;
  try
    Content.Add(Format(Cabec, [FTitle]));
    Content.Add('');
    s := '';
    y := 0;
    for i := 0 to FDataSet.FieldCount - 1 do
      if Length(FDataSet.Fields[i].DisplayLabel) > FDataSet.Fields[i].Size then
        s := s + FDataSet.Fields[i].DisplayLabel + ' \tab '
      else
        s := s + FDataSet.Fields[i].DisplayLabel + StringOfChar(' ', FDataSet.Fields[i].Size - Length(FDataSet.Fields[i].DisplayLabel)) + ' \tab ';
    s := s + '\par'#13#10;
    Content.Add(s);
    Content.Add('\b0');
    while not FDataSet.Eof do
    begin
      s := '';
      for i := 0 to FDataSet.FieldCount - 1 do
      begin
        x := Max(Length(FDataSet.Fields[i].DisplayLabel), FDataSet.Fields[i].Size);
        s := s + FDataSet.Fields[i].DisplayText + StringOfChar(' ', x - Length(FDataSet.Fields[i].DisplayText)) + ' \tab ';
      end;
      Content.Add(s + '\par');
      FDataSet.Next;
      Inc(y);
    end;
    Content.Add('\par');
    Content.Add('\par');
    Content.Add('\b Total de Registros: ' + IntToStr(y) + '\b0}');
    Content.SaveToFile(FileName);
  finally
    Finalize;
    Content.Free;
  end;
end;

procedure TDataSetExport.ExportToTXT;
var
  Content: TStrings;
  y, x, i: Integer;
  s: string;
begin
  Initialize;
  Content := TStringList.Create;
  try
    Content.Add(FTitle);
    Content.Add('');
    s := '';
    y := 0;
    for i := 0 to FDataSet.FieldCount - 1 do
      if Length(FDataSet.Fields[i].DisplayLabel) > FDataSet.Fields[i].Size then
        s := s + FDataSet.Fields[i].DisplayLabel + #32#9
      else
        s := s + FDataSet.Fields[i].DisplayLabel + StringOfChar(' ', FDataSet.Fields[i].Size - Length(FDataSet.Fields[i].DisplayLabel)) + #32#9;
    s := s + #13#10;
    for i := 0 to FDataSet.FieldCount - 1 do
      if Length(FDataSet.Fields[i].DisplayLabel) > FDataSet.Fields[i].Size then
        s := s + StringOfChar('-', Length(FDataSet.Fields[i].DisplayLabel)) + #32#9
      else
        s := s + StringOfChar('-', FDataSet.Fields[i].Size) + #32#9;
    Content.Add(s);
    while not FDataSet.Eof do
    begin
      s := '';
      for i := 0 to FDataSet.FieldCount - 1 do
      begin
        x := Max(Length(FDataSet.Fields[i].DisplayLabel), FDataSet.Fields[i].Size);
        s := s + FDataSet.Fields[i].DisplayText + StringOfChar(' ', x - Length(FDataSet.Fields[i].DisplayText)) + #32#9;
      end;
      Content.Add(s);
      FDataSet.Next;
      Inc(y);
    end;
    Content.Add('');
    Content.Add('');
    Content.Add('Total de Registros: ' + IntToStr(y));
    Content.SaveToFile(FileName);
  finally
    Finalize;
    Content.Free;
  end;
end;

procedure TDataSetExport.ExportToXML;
var
  Provider: TProvider;
  ClientDataSet: TClientDataSet;
begin
  Initialize;
  ClientDataSet := TClientDataSet.Create(Self);
  try
    Provider := TProvider.Create(Self);
    try
      Provider.DataSet := FDataset;
      Provider.Name := 'ExportProvider';
      ClientDataSet.ProviderName := 'ExportProvider';
      ClientDataSet.Open;
      ClientDataSet.SaveToFile(FFileName + '.XML', dfXML);
      ClientDataSet.Close;
    except
      Provider.Free;
    end;
  except
    ClientDataSet.Free;
  end;
  Finalize;
end;

procedure TDataSetExport.Finalize;
begin
  FDataSet.Bookmark := FBookmark;
  FDataSet.EnableControls;
end;

procedure TDataSetExport.Initialize;
begin
  FBookmark := FDataSet.Bookmark;
  FDataSet.DisableControls;
  FDataSet.First;
end;

end.
